package com.tide.crm.controller;

import java.util.Date;
import java.util.List;

import com.tide.crm.comm.BaseController;
import com.tide.crm.dto.LoginUser;
import com.tide.crm.page.table.PageTableRequest;
import com.tide.crm.service.ProjectFollowInfoService;
import com.tide.crm.utils.SnowFlakeIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tide.crm.page.table.PageTableRequest;
import com.tide.crm.page.table.PageTableHandler;
import com.tide.crm.page.table.PageTableResponse;
import com.tide.crm.page.table.PageTableHandler.CountHandler;
import com.tide.crm.page.table.PageTableHandler.ListHandler;
import com.tide.crm.dao.ProjectFollowInfoDao;
import com.tide.crm.model.ProjectFollowInfo;

import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/projectFollowInfos")
public class ProjectFollowInfoController extends BaseController {

    @Autowired
    private ProjectFollowInfoDao projectFollowInfoDao;

    @Autowired
    private ProjectFollowInfoService projectFollowInfoService;

    @PostMapping
    @ApiOperation(value = "保存")
    public ProjectFollowInfo save(@RequestBody ProjectFollowInfo projectFollowInfo, HttpServletRequest httpRequest) {
        Date date = new Date();
        LoginUser loginUser = getUserInfoByToken(httpRequest);
        SnowFlakeIdGenerator worker = new SnowFlakeIdGenerator(2);
        projectFollowInfo.setCreater(loginUser.getUsername());
        projectFollowInfo.setModifyer(loginUser.getUsername());
        projectFollowInfo.setId(worker.nextId());
        projectFollowInfo.setCreateDate(date);
        projectFollowInfo.setModifyDate(date);
        projectFollowInfoDao.save(projectFollowInfo);
        return projectFollowInfo;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    public ProjectFollowInfo get(@PathVariable Long id) {
        return projectFollowInfoDao.getById(id);
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public ProjectFollowInfo update(@RequestBody ProjectFollowInfo projectFollowInfo, HttpServletRequest httpRequest) {
        Date date = new Date();
        LoginUser loginUser = getUserInfoByToken(httpRequest);
        projectFollowInfo.setModifyer(loginUser.getUsername());
        projectFollowInfo.setModifyDate(date);
        projectFollowInfoDao.update(projectFollowInfo);
        return projectFollowInfo;
    }

    @PostMapping("/findDataList")
    @ApiOperation(value = "列表")
    public PageTableResponse list(PageTableRequest request, HttpServletRequest httpRequest) {

        LoginUser loginUser = getUserInfoByToken(httpRequest);
        request.getParams().put("creater",loginUser.getUsername());
        return new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return projectFollowInfoService.count(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<ProjectFollowInfo> list(PageTableRequest request) {
                return projectFollowInfoService.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public void delete(@PathVariable Long id) {
        projectFollowInfoDao.delete(id);
    }
}
